package cn.nawang.ebeim.api.repository.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Repository;
import cn.nawang.ebeim.api.comm.BaseQuery;
import cn.nawang.ebeim.api.comm.PageList;
import cn.nawang.ebeim.api.comm.Pages;
import cn.nawang.ebeim.api.domain.Department;

/**
 * @ClassName: DepartmentQueryImpl
 * @Description: TODO
 * @author 许炜毅
 * @date 2015年8月7日 下午5:53:25
 * @version v1.0
 */
@Repository
public class DepartmentQueryImpl extends BaseQuery {

  private static String LOAD_DEPT_COUNT = "select count(d.id) from Department d where 1=1";
  private static String LOAD_DEPT_LIST = "from Department d where 1=1";
  private static final String QUERY_DEPT_NAME =
      "from Department d where d.name=:name and d.customerId=:customerId";
  private static final String QUERY_DEPT_NAME_NOT_ID =
      "from Department d where d.name=:name and d.customerId=:customerId and d.id<>:id";
  private static final String GET_DEPT = "from Department d where d.id=:id";
  private static final String DELETE_DEPT = "delete from Department where id=:id";
  private static final String ALL_DEPT_BY_CUSTOMERID =
      "from Department d where d.customerId=:customerId";

  public PageList getDepartmentList(Pages page) {
    PageList pl = null;
    try {
      String cod = joinSearchCondition(page);// 拼接查询条件
      pl = queryListByPaging(LOAD_DEPT_COUNT + cod, LOAD_DEPT_LIST + cod, page);
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    return pl;
  }

  public List<Department> findDeptByCustomerId(String customerId) {
    try {
      Map<String, Object> paraMap = new HashMap<String, Object>();
      paraMap.put("customerId", customerId);
      return queryObject(ALL_DEPT_BY_CUSTOMERID, paraMap);
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    return null;
  }

  public Department findDeptByName(String name, String customerId) {
    try {
      Map<String, Object> paraMap = new HashMap<String, Object>();
      paraMap.put("name", name);
      paraMap.put("customerId", customerId);
      List<Department> dept = queryObject(QUERY_DEPT_NAME, paraMap);
      if (dept != null && !dept.isEmpty()) {
        return dept.get(0);
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    return null;
  }

  public Department findDeptByNameNotId(String name, int id, String customerId) {
    try {
      Map<String, Object> paraMap = new HashMap<String, Object>();
      paraMap.put("name", name);
      paraMap.put("id", id);
      paraMap.put("customerId", customerId);
      List<Department> dept = queryObject(QUERY_DEPT_NAME_NOT_ID, paraMap);
      if (dept != null && !dept.isEmpty()) {
        return dept.get(0);
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    return null;
  }

  public Department findDeptById(int deptId) {
    try {
      Map<String, Object> paraMap = new HashMap<String, Object>();
      paraMap.put("id", deptId);
      List<Department> dept = queryObject(GET_DEPT, paraMap);
      if (dept != null && !dept.isEmpty()) {
        return dept.get(0);
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    return null;
  }

  public boolean delete(int id) {
    Map<String, Object> paraMap = new HashMap<String, Object>();
    paraMap.put("id", id);
    return execHQL(DELETE_DEPT, paraMap);
  }

  public List<Department> findDeptByParentid(int parentId) {
    String hql = "from Department d where d.parentId=:parentId";
    Map<String, Object> paraMap = new HashMap<String, Object>();
    paraMap.put("parentId", parentId);
    List<Department> depts = queryObject(hql, paraMap);
    return depts;
  }
}
